package com.space.school.dao.impl;

import com.space.school.dao.RegisterDao;
import com.space.school.domain.User;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;

public class RegisterDaoImpl implements RegisterDao {
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(SpaceJDBCUtils.getDataSource());

    @Override
    public int register(String username, String password) {
//        用户名判断
        String sql_user="select username from user where username=?";
        List<User> user_list= jdbcTemplate.query(sql_user,new BeanPropertyRowMapper<>(User.class),username);
        if(user_list.size()!=0){
//            1的情况为已经注册过了
            return 1;
        }
        String sql = "insert into user (username, password,createTime) values (?, ?, ?)";
//        jdbcTemplate.update(sql, username, password, createTime);
        jdbcTemplate.update(sql, username, password,new Date());
//        3的情况为注册成功
        return 3;
    }
}
